package com.ss.android.image;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.ss.android.common.load.LRUWeakCache;
import com.ss.android.common.load.MultiAsyncLoader;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.mine.ae;

import java.io.File;
import java.util.ArrayList;

public final class AvatarLoader {
    private final int h;
    private final int expectSize;
    private final TaskInfo mTaskInfo;
    private final BaseImageManager mImageMgr;
    private final Transformer mTransformer;
    public volatile boolean b;
    public ProfileAvatarAnchor d;
    public boolean mIsNightMode;
    LRUWeakCache<String, Bitmap> mLRUWeakCache;
    boolean c;
    private MultiAsyncLoader.LoaderProxy<String, ImageInfo, Void, ImageView, Bitmap> mLoaderProxy;
    private MultiAsyncLoader mMultiAsyncLoader;
    private Drawable i;

    public AvatarLoader(int arg9, TaskInfo arg10, BaseImageManager arg11, int arg12, Transformer arg13) {
        this(arg9, arg10, arg11, arg12, arg13, 16, 2);
    }

    public AvatarLoader(int arg7, TaskInfo arg8, BaseImageManager arg9, int arg10, int arg11, boolean arg12) {
        this(arg7, arg8, arg9, arg10, new BaseTransformer(arg11, arg12));
    }

    public AvatarLoader(TaskInfo taskInfo, BaseImageManager imageMgr, int arg11, int arg12, int arg13, int arg14) {
        this(com.ss.android.article.browser.R.drawable.video_pgc_placeholder, taskInfo, imageMgr, arg11, new BaseTransformer(arg12, true), arg13, arg14);
    }

    private AvatarLoader(int arg4, TaskInfo taskInfo, BaseImageManager imageMgr, int expectSize, Transformer transformer, int capacity, int maxWorker) {
        this.h = arg4;
        this.expectSize = expectSize;
        this.mTaskInfo = taskInfo;
        this.mImageMgr = imageMgr;
        this.mTransformer = transformer;
        this.mLRUWeakCache = new LRUWeakCache(32);
        this.mLoaderProxy = new AvatarLoaderProxy(this);
        this.mMultiAsyncLoader = new MultiAsyncLoader(capacity, maxWorker, this.mLoaderProxy);
        this.b = true;
        this.c = true;
    }

    public final void onPause() {
        this.c = true;
        this.mMultiAsyncLoader.resume();
    }

    public final void loadAvatar(ImageView imageView, String uri) {
        ImageInfo imageInfo = new ImageInfo(uri, null);
        if (imageView != null) {
            String key = imageInfo.mKey;
            this.i = imageView.getBackground();
            imageView.setTag(key);
            if (key == null) {
                this.placeHolder(imageView);
            } else {
                Bitmap bitmap = this.mLRUWeakCache.get(key);
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                    imageView.setBackgroundDrawable((null));
                    new ArrayList().add(imageView);
                    if (this.d != null) {
                        this.d.a(bitmap);
                    }
                    imageView.setTag(null);
                } else {
                    this.placeHolder(imageView);
                    this.mMultiAsyncLoader.addTask(key, imageInfo, null, imageView);
                }
            }
        }
    }

    public final void clear(ImageView imageView) {
        if (imageView != null) {
            if (imageView != null) {
                Object tag = imageView.getTag();
                if ((tag instanceof String) && !android.support.design.a.isTextEmpty((String) tag)) {
                    this.mMultiAsyncLoader.cancelTask(imageView.getTag(), imageView);
                }
            }
            imageView.setTag(null);
            imageView.setImageDrawable(null);
        }
    }

    final Bitmap loadAvatar(String key, ImageInfo imageInfo) {
        try {
            String externalCacheFilePath = this.mImageMgr.getExternalCacheFile(key);
            String internalCacheFilePath = this.mImageMgr.getInternalCacheFile(key);
            File externalCacheFile = new File(externalCacheFilePath);
            File internalCacheFile = new File(internalCacheFilePath);
            boolean isCacheFileExists = externalCacheFile.isFile() || internalCacheFile.isFile();
            if (!isCacheFileExists && (this.b)) {
                isCacheFileExists = com.ss.android.image.m.a().downloadImage(
                        null,
                        512000,
                        imageInfo.mUri,
                        imageInfo.mUrlList,
                        this.mImageMgr.getExternalImageSaveDir(key),
                        this.mImageMgr.getInternalImageSaveDir(key),
                        BaseImageManager.getCacheFileName(key),
                        null, this.mTaskInfo);
            }

            if (!isCacheFileExists) {
                return null;
            }

            String cacheFilePath;
            if (!new File(externalCacheFilePath).isFile()) {
                cacheFilePath = internalCacheFilePath;
            } else {
                cacheFilePath = externalCacheFilePath;
            }

            Bitmap bitmap = android.support.design.a.decodeFile(cacheFilePath, this.expectSize, this.expectSize, null);
            if (bitmap == null) {
                return null;
            }

            if (this.mTransformer == null) {
                return bitmap;
            }

            bitmap = this.mTransformer.transform(bitmap);
            return bitmap;
        } catch (Throwable throwable) {
            Logger.d("AvatarLoader", "loadAvatar exception " + throwable);
            return null;
        }
    }

    public final void onStop() {
        this.mMultiAsyncLoader.pause();
        this.mLRUWeakCache.shrink();
    }

    private void placeHolder(ImageView imageView) {
        if (this.h > 0) {
            imageView.setImageResource(com.ss.android.image.m.a().a(this.h, this.mIsNightMode));
        } else if (this.i != null) {
            imageView.setBackgroundDrawable(this.i);
        }
    }

    public final void onDestroy() {
        this.c = false;
        this.mMultiAsyncLoader.stop();
        if (this.mTaskInfo != null) {
            this.mTaskInfo.a = true;
        }
    }

    public interface Transformer {
        Bitmap transform(Bitmap bitmap);
    }

    public static class BaseTransformer implements Transformer {
        private final int roundCornerPixel;
        private final boolean isRound;

        public BaseTransformer(int roundCornerPixel, boolean isRound) {
            this.roundCornerPixel = roundCornerPixel;
            this.isRound = isRound;
        }

        public Bitmap transform(Bitmap bitmap) {
            if (this.roundCornerPixel > 0) {
                bitmap = this.isRound
                        ? BaseImageManager.createRoundBitmap(bitmap, this.roundCornerPixel)
                        : BaseImageManager.createRoundRectBitmap(bitmap, this.roundCornerPixel);
            }
            return bitmap;
        }
    }

    public static final class ProfileAvatarAnchor {
        private final ae a;

        public ProfileAvatarAnchor(ae arg1) {
            this.a = arg1;

        }

        public final void a(Bitmap bitmap) {
            if (!this.a.isDestroyed() && bitmap != null) {
                this.a.b.setBackgroundResource(0);
            }
        }
    }
}

